package com.easyshopping;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.easyshopping.domain.ItemCategory;
import com.easyshopping.facade.ShoppingListFacade;

public class EditActivity extends Activity implements OnClickListener {

	private Button btn_OK;
	private Button btn_Cancel;
	private EditText et_name;

	private ItemCategory itemCate;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.edit);

		Bundle extras = getIntent().getExtras();
		int itemCate_id = ((Integer) extras.get(ItemCategory.ITEMCATEGORY_ID))
				.intValue();

		itemCate = ShoppingListFacade.getInstance().fetchItemCategory(
				itemCate_id);

		btn_OK = (Button) this.findViewById(R.id.Btn_Edit_OK);
		btn_Cancel = (Button) this.findViewById(R.id.Btn_Edit_Cancel);
		et_name = (EditText) this.findViewById(R.id.Page_Edit_EditText);

		et_name.setText(itemCate.getName());

		btn_OK.setOnClickListener(this);
		btn_Cancel.setOnClickListener(this);
	}

	public void onClick(View v) {

		if (v.getId() == R.id.Btn_Edit_OK) {

			if (et_name.getText().toString().trim().length() == 0){
				et_name.setHint(R.string.cantbeempty);
				return;
			}
			
			itemCate.setName(et_name.getText().toString());
			ShoppingListFacade.getInstance().updateItemCategory(itemCate);
			this.finish();
			return;
		}

		if (v.getId() == R.id.Btn_Edit_Cancel) {
			this.finish();
			return;
		}

	}

}
